<?php

namespace app\common\model;

use app\admin\model\order\Goods;
use app\common\model\order\Goods as OrderGoods;
use think\Model;
use traits\model\SoftDelete;

class Order extends Model
{

    use SoftDelete;



    // 表名
    protected $name = 'order';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'paytime_text',
        'createtime_text',
        'status_text',
        'canceltime_text',
        'endtime_text',
        'revenue_text',
    ];

    public function getRevenueTextAttr($value, $data)
    {
        $revenue  = bcmul($data['total'], config('site.service_commission'), 2);
        return config('site.service_commission');
    }
    public function getPickRevenueTextAttr($value, $data)
    {
        $revenue  = bcmul($data['total'], config('site.picker_commission'), 2);
        return config('site.picker_commission');
    }
    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1'), '2' => __('Status 2'), '3' => __('Status 3'), '4' => __('Status 4'), '5' => __('Status 5'), '6' => __('Status 6'), '7' => __('Status 7'), '8' => __('Status 8'), '-1' => __('Status -1')];
    }
    public function getEndtimeTextAttr($value, $data)
    {
        return $data['createtime'] + 900;
    }

    public function getCreatetimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['createtime']) ? $data['createtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function getPaytimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['paytime']) ? $data['paytime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getCanceltimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['canceltime']) ? $data['canceltime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setPaytimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setCanceltimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function address()
    {
        return $this->belongsTo('app\common\model\user\Address', 'address_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function goods()
    {
        return $this->hasMany('app\common\model\order\Goods', 'order_id', 'id');
    }

    public function user()
    {
        return $this->belongsTo('User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function retrieveInfo()
    {
        return $this->belongsTo('app\common\model\User', 'retrieve', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function pickInfo()
    {
        return $this->belongsTo('app\common\model\User', 'pick', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function cleanInfo()
    {
        return $this->belongsTo('app\common\model\User', 'clean', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function shop()
    {
        return $this->belongsTo('Shop', 'shop_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public  function addData($data,  $uid)
    {
        $list = $data['goods'];
        $array = [
            'user_id' => $uid,
            'total_money' =>  $data['total_amount'],
            'pay_money' =>  $data['pay_price'],
            'coupon_id' =>  $data['coupon_id'],
            'discount_money' =>  $data['discount_amount'],
            'sn' => sn('order', 'sn', 'XX'),
            'shop_id' => $data['shop_id'],
            'address_id' => $data['address_id'],
            'content' => $data['content'] ?? '',
            'total' => $data['goods_num'] ?? 0,
        ];
        $res = $this->allowField(true)->save($array);
        if ($res) {
            foreach ($list as $vo) {
                $info = [
                    'order_id' => $this->id,
                    'goods_id' => $vo['goods_id'],
                    'goods_num' => $vo['num'],
                    'goods_name' => $vo['goods']['title'],
                    'goods_price' => $vo['goods']['price'],
                    'total_price' => $vo['goods']['price'] * $vo['num'],
                    'image' => $vo['goods']['image'],
                ];
                $infos = new OrderGoods();
                $infos->isUpdate(false)->save($info);
            }
        }
        return $this->id;
    }
}
